/*
 * TMap Designer - A Thematic Map design software
 * Copyright (C) 2006 World Health Organization
 * http://www.who.int
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */
package who.tmap.exp;

import java.io.File;
import java.io.FileOutputStream;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFDataFormat;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import who.tmap.bo.AttrDef;
import who.tmap.bo.Attrs;
import who.tmap.common.TMapException;

public class ExcelExportAdapter implements ExportAdapter {
	
	private static final String SHEET_NAME ="Data";
	private static final String DEFAULT_DATE_FORMAT ="m/d/yy";

	private HSSFWorkbook wb;
	private HSSFSheet ws;
	
	public ExcelExportAdapter() {
	}
	
	public void export( File file, Attrs attrs ) throws TMapException {
		try {
			wb =new HSSFWorkbook();
			ws =wb.createSheet( SHEET_NAME );
			
			writeHeader( attrs.getHeader() );
			writeRows( attrs );
			
			FileOutputStream fos = new FileOutputStream( file );
		    wb.write(fos);
		    fos.close();
		} catch( Exception e ) {
			throw new TMapException( e );
		}
	}
	
	private void writeHeader( List<AttrDef> attrDefs ) {
		HSSFRow row =ws.createRow(0);
		
		for( short colCt =0; colCt < attrDefs.size(); colCt++ ) {
			HSSFCell cell =row.createCell( colCt );
			cell.setCellValue( attrDefs.get( colCt ).getName() );
			//cell.setCellStyle( HSSFCellStyle.ALIGN_CENTER );
		}
	}
	
	@SuppressWarnings("unchecked")
	private void writeRows( Attrs attrs ) {
		List<AttrDef> defs =attrs.getHeader();
		
		List<List> rows =attrs.getRows();
		for( short rowCt =0; rowCt < rows.size(); rowCt++ ) {
			HSSFRow hssfRow =ws.createRow( rowCt + 1 );	
			List<Object> row =rows.get( rowCt );
		
			for( short colCt =0; colCt < row.size(); colCt++ ) {
				AttrDef attrDef =defs.get( colCt );
				HSSFCell cell =hssfRow.createCell( colCt );
				
				Object obj =row.get( colCt );
				if ( obj instanceof String ) {
					cell.setCellValue( (String)obj );
				} else if ( obj instanceof Double ) {
					cell.setCellValue( (Double)obj );
				} else if ( obj instanceof Float ) {
					cell.setCellValue( (Float)obj );	
				} else if ( obj instanceof BigDecimal ) {
					cell.setCellValue( ((BigDecimal)obj).doubleValue() );	
				} else if ( obj instanceof Long ) {
					cell.setCellValue( (Long)obj );	
				} else if ( obj instanceof Integer ) {
					cell.setCellValue( (Integer)obj );	
				} else if ( obj instanceof Short ) {
					cell.setCellValue( (Short)obj );						
				} else if ( obj instanceof Date ) {
					HSSFCellStyle cellStyle = wb.createCellStyle();
					cellStyle.setDataFormat(HSSFDataFormat.getBuiltinFormat( DEFAULT_DATE_FORMAT ));
					cell.setCellValue( (Date)obj );						
				} else if ( obj != null ) {
					cell.setCellValue( obj.toString() );
				}
			}
		}
	}
}
